﻿
var siginfo = {
    sig:"",
    sigVer:"1",
    nonce:"",
    ts:""
}
if(!window.Secret){
    window.Secret = {};
}

//获取sig
window.Secret.GetSig = function(method, path, params){
    var paramsTostr = GetParamsToString(params);
    var unifiedString = method.toUpperCase() + ":" + path + ":" + paramsTostr;
    var sighash = CryptoJS.HmacSHA1(unifiedString, params["loginCode"]);
    var sigBase64 = CryptoJS.enc.Base64.stringify(sighash);
    return sigBase64;
}

//设定随机数跟时间
window.Secret.SetNonceAndTime = function(){
    siginfo.ts = CurentTimeISO8061();
    siginfo.nonce = randomWord(16);
}

//将所有参数按照规则拼接为字符串
function GetParamsToString(params){

    //siginfo.ts = CurentTimeISO8061();
    //siginfo.nonce = randomWord(16);

    params["sigVer"] = siginfo.sigVer;
    params["ts"] = siginfo.ts;
    params["nonce"] = siginfo.nonce;

    var strparam = "";

    params = SortParams(params);
    for(var key in params){
        if(typeof(params[key])=="function"){
            continue;
        }
        if(params[key] !== null && params[key] !== "" && params[key] !== undefined){
            if(strparam === ""){
                strparam += key + "=" + params[key];
            }else{
                strparam += "&" + key + "=" + params[key];
            }
        }
    }
    return strparam;
}

//所有参数按照字母表顺序重新排序
function SortParams(params){
    var newParams = {};
    var tempAry = [];
    for(var key in params){
        tempAry.push(key);
    }

    tempAry.sort();
    for(var i=0;i<tempAry.length;i++){
        newParams[tempAry[i]] = params[tempAry[i]];
    }

    return newParams;
}


//获取当前ISO 8061 格式时间
function CurentTimeISO8061()
{
	var now = new Date();

	var year = now.getFullYear();       //年
	var month = now.getMonth() + 1;     //月
	var day = now.getDate();            //日

	var hh = now.getHours();            //时
	var mm = now.getMinutes();          //分
	var ss = now.getSeconds();
	var fff = now.getMilliseconds();

	(month < 10) && (month = "0"+month);

	(day < 10) && (day = "0"+day);

	(hh < 10) && (hh = "0"+hh);

	(mm < 10) && (mm = '0'+mm);

	(ss < 10) && (ss = '0'+ss);

	(fff < 10)?(fff = '00'+fff): ((fff < 100)?(fff = '0'+fff):(null));

	return year.toString()+"-"+month+"-"+day+"T"+hh+":"+mm+":"+ss+"."+fff;
}

//产生随机数
function randomWord(len){
    var str = "",
        range = len,
        arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

    for(var i=0; i<range; i++){
        pos = Math.round(Math.random() * (arr.length-1));
        str += arr[pos];
    }
    return str;
}

//字节组base64加密
function ArrayBufferToBase64( buffer ) {
	var binary = '';
	var bytes = new Uint8Array( buffer );
	var len = bytes.byteLength;
	for (var i = 0; i < len; i++) {
		binary += String.fromCharCode( bytes[ i ] );
	}
	return window.btoa( binary );
}


//16进制字符串转化字节组
function StrToBytes(str)
{
	var pos = 0;
	var len = str.length;
	if(len %2 != 0)
	{
	   return null;
	}
	len /= 2;
	var hexA = new Array();
	for(var i=0; i<len; i++)
	{
	   var s = str.substr(pos, 2);
	   var v = parseInt(s, 16);
	   hexA.push(v);
	   pos += 2;
	}
	return hexA;
}